<template>
	<view class="container">
		<uni-section :title="sectionTitle" type="line">
			<view style="padding: 10px;">
				<uni-easyinput
					:disabled="!editableReminder"
					type="textarea"
					autoHeight
					trim="all"
					v-model="content"
					placeholder="请输入提醒内容"
					@change="change"
				></uni-easyinput>
			</view>
			<view class="content" style="font-size: 14px;">提醒时间</view>
			<uni-datetime-picker :clear-icon="true" :border="false" :disabled="!editableReminder" type="datetime" v-model="time" @change="change" />

			<view style="padding: 10px;text-align: right;">
				<button type="primary" plain="true" size="mini" @click="submit">{{ btnTextReminder }}</button>
			</view>
			<view style="padding: 10px;text-align: right;"><button type="primary" plain="true" size="mini" @click="del">删除</button></view>
		</uni-section>
	</view>
</template>

<script>
export default {
	data() {
		return {
			edit: false,
			content: '',
			time: '',
			id: 1,
			activityId: 1,
			activityName: '',
			editableReminder: false,
			btnTextReminder: '编辑',
			changedReminder: false,
			sectionTitle: '',

			apiUrl: 'http://localhost:8080/'
		};
	},
	onLoad: function(option) {
		this.activityId = option.activityId;
		this.activityName = option.activityName;
		if (option.edit == 'true') {
			this.edit = true;
			this.content = option.content;
			this.time = option.time;
			this.id = option.id;

			this.sectionTitle = '编辑提醒';
		} else {
			this.sectionTitle = '发布提醒';
		}
		uni.setNavigationBarTitle({
			title: this.activityName
		});
	},
	methods: {
		change() {
			this.changedReminder = true;
		},
		del() {
			uni.request({
				method: 'DELETE',
				url: this.apiUrl + 'reminder/' + this.id,
				header: {
					Authorization:"Bearer " + uni.getStorageSync('token')
				},
				data: {},
				success: res => {
					console.log(res.data);
					if (res.statusCode == 200) {
						uni.showToast({
							icon: 'success',
							title: '提醒删除成功'
						});
						uni.navigateBack({
							delta: 1
						});
					}
					if (res.statusCode == 400) {
						uni.showToast({
							icon: 'error',
							title: JSON.parse(res.data).message
						});
					}
				},
				fail: err => {
					reject(err);
					return uni.showToast({
						icon: 'loading',
						title: '请求失败'
					});
				}
			});
		},
		submit() {
			if (this.btnTextReminder == '编辑') {
				this.editableReminder = true;
				this.btnTextReminder = '确认';
			} else if (this.time == '' || this.content == '') {
				uni.showToast({
					title: '时间和提醒内容皆不可为空！',
					icon: 'error',
					duration: 2000
				});
			} else {
				if (this.time.length == 11) {
					// 统一前后端时间格式，默认上午9点提醒
					this.time += '09:00:00';
				}
				this.editableReminder = false;
				this.btnTextReminder = '编辑';
				if (this.changedReminder) {
					if (this.edit == false) {
						uni.request({
							method: 'POST',
							url: this.apiUrl + 'reminder',
							header: {
								Authorization:"Bearer " + uni.getStorageSync('token')
							},
							data: {
								activityId: this.activityId,
								time: this.time,
								content: this.content
							},
							success: res => {
								console.log(res.data);
								if (res.statusCode == 200) {
									uni.showToast({
										icon: 'success',
										title: '提醒发布成功'
									});
								}
								if (res.statusCode == 400) {
									uni.showToast({
										icon: 'error',
										title: JSON.parse(res.data).message
									});
								}
							},
							fail: err => {
								reject(err);
								return uni.showToast({
									icon: 'loading',
									title: '请求失败'
								});
							}
						});
					} else {
						uni.request({
							method: 'PUT',
							url: this.apiUrl + 'reminder',
							header: {
								Authorization:"Bearer " + uni.getStorageSync('token')
							},
							data: {
								id: this.id,
								activityId: this.activityId,
								time: this.time,
								content: this.content
							},
							success: res => {
								console.log(res.data);
								if (res.statusCode == 200) {
									uni.showToast({
										icon: 'success',
										title: '提醒修改成功'
									});
								}
								if (res.statusCode == 400) {
									uni.showToast({
										icon: 'error',
										title: JSON.parse(res.data).message
									});
								}
							},
							fail: err => {
								reject(err);
								return uni.showToast({
									icon: 'loading',
									title: '请求失败'
								});
							}
						});
					}
				}
				this.changedReminder = false;
			}
		}
	}
};
</script>

<style>
.container {
	padding: 20px;
	font-size: 14px;
	line-height: 24px;
}
.content {
	padding: 15px;
}
</style>
